package com.cty.redis;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 * @Auther: cty
 * @Date: 2020/4/8 20:00
 * @Description:  操作redis集群（无密码）
 * @version: 1.0
 */
public class TestRedisCluster {
    JedisCluster cluster = null;

    @Before
    public void beforeRedis(){
        // 配置Redis信息
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(30);
        config.setMaxIdle(10);
        config.setMaxWaitMillis(1500);
        config.setTestOnBorrow(true);
        config.setTestOnReturn(true);

        Set<HostAndPort> set = new HashSet<>();
        set.add(new HostAndPort("192.168.238.128",7001));
        set.add(new HostAndPort("192.168.238.128",7002));
        set.add(new HostAndPort("192.168.238.128",7003));
        set.add(new HostAndPort("192.168.238.128",7004));
        set.add(new HostAndPort("192.168.238.128",7005));
        set.add(new HostAndPort("192.168.238.128",7006));
        cluster = new JedisCluster(set,3000,3000,10,config);
        System.out.println("beforeRedis：已连接Redis集群");
    }

    @Test
    public void myRedis(){
        // 增
        System.out.println(cluster.set("address","山东"));
        // 查
        System.out.println(cluster.get("address"));
//        // 改
//        System.out.println(cluster.set("address","湖北"));
//        // 删
//        System.out.println(cluster.del("address"));
    }

    @After
    public void afterRedis(){
        try {
            cluster.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("afterRedis：关闭Redis集群连接失败");
        }
        System.out.println("afterRedis：已关闭Redis集群连接");
    }
}
